package link

import (
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/Core"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/App/admin"
)

type Admin struct {

}

func (_ Admin) Setup() {
	Core.SetRoute("GET:/admin/links/add", "[I]link.Admin.AddAction")
	Core.SetRoute("POST:/admin/links/add", "[I]link.Admin.PostAction")
	Core.SetRoute("GET:/admin/links/edit", "[I]link.Admin.EditAction")
	Core.SetRoute("POST:/admin/links/edit", "[I]link.Admin.UpdateAction")
	Core.SetRoute("DELETE:/admin/links/delete", "[I]link.Admin.DeleteAction")
	Core.SetRoute("GET:/admin/links/approve", "[I]link.Admin.ApproveAction")
	Core.SetRoute("GET:/admin/links", "[I]link.Admin.ListAction")
}

func (_ Admin) ListAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	data := make(map[string]interface {})
	data["Links"] = GetList()
	response.Body = admin.RenderAdminPage("link/list.html", data, nil, "")
}

func (_ Admin) AddAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	response.Body = admin.RenderAdminPage("link/add.html", nil, nil, "")
}

func (_ Admin) PostAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	data := request.Input().All()
	res := map[string]interface {}{"res":false}
	msg := validateLinkData(data, "")
	if msg != "" {
		res["msg"] = msg
		response.Json(res)
		return
	}
	AddLink(data)
	res["res"] = true
	response.Json(res)
}

func (_ Admin) EditAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if len(request.Param) < 5 {
		response.Redirect("/admin/links/")
		return
	}
	id := request.Param[4]
	data := make(map[string]interface {})
	data["Link"] = GetLinkById(id)
	response.Body = admin.RenderAdminPage("link/edit.html", data, nil, "")
}

func (_ Admin) ApproveAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if len(request.Param) < 5 {
		response.Redirect("/admin/links/")
		return
	}
	id := request.Param[4]
	ApproveLink(id)
	response.Redirect("/admin/links/")
}

func (_ Admin) UpdateAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if len(request.Param) < 5 {
		response.Status = 400
		return
	}
	id := request.Param[4]
	data := request.Input().All()
	res := map[string]interface {}{"res":false}
	msg := validateLinkData(data, id)
	if msg != "" {
		res["msg"] = msg
		response.Json(res)
		return
	}
	UpdateLink(id, data)
	res["res"] = true
	response.Json(res)
}


func (_ Admin) DeleteAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if len(request.Param) < 5 {
		response.Status = 400
		return
	}
	id := request.Param[4]
	DeleteLink(id)
	res := map[string]interface {}{"res":true}
	response.Json(res)
}

